const http = require('../../../utils/httpUtils.js');
const app = getApp()
const RSA = require("../../../utils/wx_rsa.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '支付' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        keyBoard: true,
        pwdPay: '',
        userInfo: '',
        payType: -1,
        keyNum: ['1', '4', '7', '2', '5', '8', '3', '6', '9'],
        yeIcon: '../../../images/icon/topay1.png',
        wxIcon: 'https://app.cgxczx.com/weixin/images/icon/wx_pay.png'
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //保存支付订单信息
        this.setData({
            payInfo: JSON.parse(options.payInfo)
        })
    },
    //支付方式
    payType(e) {
        this.setData({
            payType: e.currentTarget.id
        })
    },
    toPay() {
        switch (this.data.payType) {
            case -1:
                wx.showToast({
                    title: '请选择支付方式',
                    icon: 'none'
                })
                break
            case '0':
                this.buyorder()
                break
            case '1':
                this.wxPay()
                break
        }
    },
    //微信支付
    wxPay() {
        let data;
        if (app.globalData.openId) {
            data = {
                open_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${app.globalData.openId}`)),
                c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`weixinSmall`)),
                pay_type: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("WxPay")),
                opa_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.payInfo.opa_id}`))
            }
        } else {
            try {
                const value = wx.getStorageSync('openid')
                if (value) {
                    data = {
                        open_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${value}`)),
                        c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`weixinSmall`)),
                        pay_type: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("WxPay")),
                        opa_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.payInfo.opa_id}`))
                    }
                }
            } catch (e) {
                wx.showToast({
                    title: '没有获取到openid',
                    icon: 'none'
                })
            }
        }

        //调用支付订单接口
        http._headerPost('/api/Payment/payment', data, res => {
            if (res.data.code == 1) {
                //调起支付
                let pay = res.data.data.prepay_order;
                wx.requestPayment({
                    timeStamp: pay.timeStamp + '',
                    nonceStr: pay.nonceStr,
                    package: pay.package,
                    signType: "MD5",
                    paySign: pay.paySign,
                    success: res => {
                        //转到
                        setTimeout(() => {
                            wx.reLaunch({
                                url: '/pages/public/pay-ok/index',
                            })
                        }, 200)
                    },
                    //支付失败
                    fail: err => {
                        console.log(err)
                        wx.showModal({
                            title: '提示',
                            content: '支付失败',
                            showCancel: false,
                        })
                    }
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 3000
                })
            }
        })
    },
    //判断是否设置支付密码
    buyorder() {
        http._headerPost('/index/User/checkhavepaypwd', '', res => {
            if (res.data.code > 0) {
                this.hiddKeyBoard()
            } else {
                wx.showModal({
                    title: '提示',
                    confirmText: '去设置',
                    content: '您还未设置支付密码',
                    success(res) {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: '../set-paypwd/index',
                            })
                        } else {
                            console.log('暂不')
                        }
                    }
                })
            }
        })
    },
    hiddKeyBoard() {
        this.setData({
            keyBoard: false
        })
    },
    nohiddKeyBoard() {
        this.setData({
            keyBoard: true
        })
    },
    //键盘输入事件
    numClick(e) {
        if (e.currentTarget.id == 'cls') {
            let num = this.data.pwdPay
            this.setData({
                pwdPay: num.substring(0, num.length - 1)
            })
        } else if (e.currentTarget.id == 'del') {
            this.setData({
                pwdPay: ''
            })
        } else if (this.data.pwdPay.length < 6) {
            this.setData({
                pwdPay: this.data.pwdPay + e.currentTarget.id
            })
        }
        if (this.data.pwdPay.length == 6) {
            http._headerPost('/index/user/checkpaypwd', {
                password: this.data.pwdPay
            }, res => {
                if (res.data.code == 1) {
                    this.balanPay()
                    this.nohiddKeyBoard()
                    this.setData({
                        pwdPay: ''
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel: false
                    })
                    this.setData({
                        pwdPay: ''
                    })
                }
            })
        }
    },
    //忘记密码
    forgetPwd() {
        wx.navigateTo({
            url: '../../Three/set-paypwd/setpaypwd',
        })
    }
})